﻿/*
This project and all of its code is/are Copyright (C) 2017  Nils Hunter Alving

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;

namespace UnitTestSTK
{
    /// <summary>
    /// Extends the assertion capabilities of unit tests
    /// </summary>
    public static class AssertThat
    {
        /// <summary>
        /// Validates that a specific action throws the expected exception type
        /// The original implementation for this method was written by someone else (not sure who)
        /// who was nice enough to share it on SO. Great idea!!
        /// </summary>
        /// <typeparam name="T">The type of exception to be thrown</typeparam>
        /// <param name="action">The action that should throw the exception</param>
        public static void Throws<T>(Action action)
        {
            try
            {
                action();
            }
            catch (Exception e)
            {
                if (e.GetType() != typeof(T))
                {
                    throw new Exception($"Expected exception of type: {typeof(T)}, instead got: {e.GetType()}");
                }
                return;
            }
            throw new Exception($"Expected exception of type: {typeof(T)} but none was thrown");
        }
    }
}
